package tmproject.hlhj.fhp.cartrans.activitys

import android.animation.ObjectAnimator
import android.animation.ValueAnimator
import android.graphics.Color
import android.text.TextUtils
import android.util.Log
import android.widget.LinearLayout
import kotlinx.android.synthetic.main.aty_carinfo.*
import tmproject.hlhj.fhp.cartrans.BaseAty
import tmproject.hlhj.fhp.cartrans.IView.CarView
import tmproject.hlhj.fhp.cartrans.R
import tmproject.hlhj.fhp.cartrans.customVIews.ImgCheckDialog
import tmproject.hlhj.fhp.cartrans.javaBean.CarInfoBean
import tmproject.hlhj.fhp.cartrans.log
import tmproject.hlhj.fhp.cartrans.presenter.CarPresneter
import tmproject.hlhj.fhp.cartrans.utils.EmptyUtils
import tmproject.hlhj.fhp.cartrans.utils.SpHelper
import tmproject.hlhj.fhp.cartrans.utils.TimeUtils
import kotlin.math.E

class CarinfoAty:BaseAty<CarView,CarPresneter>(), CarView {
    private var isCarMan=false
    private var isCarHead=false
    private var isGuaChe=false
    private var carManHegith=405
    private var carHeadHeight=405
    private var isfirst=true
    private var guacheHeight=0

    override fun getContentId(): Int {
        return R.layout.aty_carinfo
    }

    override fun setCarInfo(carInfoBean: CarInfoBean) {
        val bean = carInfoBean.data
        if (!EmptyUtils.isTextEmpty(bean.owner_name)){
            ownerName.text = bean.owner_name
        }
        if (!EmptyUtils.isTextEmpty(bean?.owner_tel)){
            userPhone.text=bean?.owner_tel
        }
        if (!EmptyUtils.isTextEmpty(bean?.owner_id_no)){
            userId.text=bean?.owner_id_no
        }

        if (!EmptyUtils.isTextEmpty(bean?.tractor_no)){
            carHeadNum.text=bean?.tractor_no
        }
        if (!EmptyUtils.isTextEmpty(bean?.tractor_pic)){
                imgCar.text="查看"
            //todo 设置点击事件
            imgCar.setOnClickListener {
                ImgCheckDialog(this,bean?.tractor_pic,"车头照片","").show()
            }
        }

        if (!EmptyUtils.isTextEmpty(bean?.tractor_factory_model)){
            carHeadType.text=bean?.tractor_factory_model
        }

        if (!EmptyUtils.isTextEmpty(bean?.tractor_engine_no)){
            carHeadEngin.text=bean?.tractor_engine_no
        }
        if (!EmptyUtils.isTextEmpty(bean?.tractor_frame_no)){
            carHeadFram.text=bean?.tractor_frame_no
        }

        if (!EmptyUtils.isTextEmpty(bean?.tractor_passport_date)){
            carHeadTime1.text=bean?.tractor_passport_date
        }
        if (!EmptyUtils.isTextEmpty(bean?.tractor_passport_expire_date)){
            carHeadTime2.text=bean?.tractor_passport_expire_date
        }
        if (!EmptyUtils.isTextEmpty(bean?.tractor_passport_pic)){
            imgCarHeadXingChe.text="查看"
            if (TextUtils.isEmpty(bean?.tractor_passport_expire_date)){

            }else{
                if (TimeUtils.timeLeft(TimeUtils.getTime(bean?.tractor_passport_expire_date))){
                    imgCarHeadXingChe.text="即将到期"
                    imgCarHeadXingChe.setTextColor(Color.RED)
                }
            }

            //todo 点击事件
            imgCarHeadXingChe.setOnClickListener {
                ImgCheckDialog(this,bean?.tractor_passport_pic,"行车证",bean?.tractor_passport_expire_date).show()
            }

        }
        if (!EmptyUtils.isTextEmpty(bean?.tractor_operation_no)){
            车头营运证号.text=bean?.tractor_operation_no
        }

        if (!EmptyUtils.isTextEmpty(bean?.tractor_operation_date)){
            车头营运证注册时间.text=bean?.tractor_operation_date
        }
        if (!EmptyUtils.isTextEmpty(bean?.tractor_operation_expire_date)){
            车头营运证到期时间.text=bean?.tractor_operation_expire_date
        }

        if (!EmptyUtils.isTextEmpty(bean?.tractor_operation_pic)){
            //todo 点击事件
            车头运营证点击.text="查看"
            if (TextUtils.isEmpty(bean?.tractor_operation_expire_date)){

            }else{
                if (TimeUtils.timeLeft(TimeUtils.getTime(bean?.tractor_operation_expire_date))){
                    车头运营证点击.text="即将到期"
                    车头运营证点击.setTextColor(Color.RED)
                }
            }

            车头运营证点击.setOnClickListener {
                ImgCheckDialog(this,bean?.tractor_operation_pic,"运营证",bean?.tractor_operation_expire_date).show()
            }

        }
        if (!EmptyUtils.isTextEmpty(bean?.tractor_insurance_company)){
            车头交强险所属公司.text=bean?.tractor_insurance_company
        }
        if (!EmptyUtils.isTextEmpty(bean?.tractor_insurance_expire_date)){
            车头交强险到期日期.text=bean?.tractor_insurance_expire_date
        }
        if (!EmptyUtils.isTextEmpty(bean?.tractor_insurance_pic)){
            //todo 点击
            车头交强险点击.text="查看"
            if (TextUtils.isEmpty(bean?.tractor_insurance_expire_date)){

            }else{
                if (TimeUtils.timeLeft(TimeUtils.getTime(bean?.tractor_insurance_expire_date))){
                    车头交强险点击.text="即将到期"
                    车头交强险点击.setTextColor(Color.RED)
                }
            }

            车头交强险点击.setOnClickListener {
                ImgCheckDialog(this,bean?.tractor_insurance_pic,"车头交强险",bean?.tractor_insurance_expire_date).show()
            }

        }

        if (!EmptyUtils.isTextEmpty(bean?.tractor_commercial_insurance_expire_date)){
            车头商业险到期日期.text=bean?.tractor_commercial_insurance_expire_date
        }
        if (!EmptyUtils.isTextEmpty(bean?.tractor_commercial_insurance_pic)){
                    //todo
            车头商业险点击.text="查看"
            if (TextUtils.isEmpty(bean?.tractor_commercial_insurance_expire_date)){

            }else{
                if (TimeUtils.timeLeft(TimeUtils.getTime(bean?.tractor_commercial_insurance_expire_date))){
                    车头商业险点击.text="即将到期"
                    车头商业险点击.setTextColor(Color.RED)
                }
            }
            车头商业险点击.setOnClickListener {
                ImgCheckDialog(this,bean?.tractor_commercial_insurance_pic,"车头商业险",bean?.tractor_commercial_insurance_expire_date).show()

            }

        }

        if (!EmptyUtils.isTextEmpty(bean?.tractor_carrier_insurance_expire_date)){
            承运人责任险到期日期.text=bean?.tractor_carrier_insurance_expire_date
        }
        if (!EmptyUtils.isTextEmpty(bean?.tractor_carrier_insurance_pic)){
            //todo
            承运人点击.text="查看"
            if (TextUtils.isEmpty(bean?.tractor_carrier_insurance_expire_date)){

            }else{
                if (TimeUtils.timeLeft(TimeUtils.getTime(bean?.tractor_carrier_insurance_expire_date))){
                    承运人点击.text="即将到期"
                    承运人点击.setTextColor(Color.RED)
                }
            }

            承运人点击.setOnClickListener {
                ImgCheckDialog(this,bean?.tractor_commercial_insurance_pic,"承运人",bean?.tractor_carrier_insurance_pic).show()
            }

        }
        if (!EmptyUtils.isTextEmpty(bean?.tractor_employer_insurance_expire_date)){
            雇主责任险到期时间.text=bean?.tractor_employer_insurance_expire_date
        }
        if (!EmptyUtils.isTextEmpty(bean?.tractor_employer_insurance_pic)){
            //todo
            车头雇主责任险点击.text="查看"
            if (TextUtils.isEmpty(bean?.tractor_employer_insurance_expire_date)){

            }else{
                if (TimeUtils.timeLeft(TimeUtils.getTime(bean?.tractor_employer_insurance_expire_date))){
                    车头雇主责任险点击.text="即将到期"
                    车头雇主责任险点击.setTextColor(Color.RED)
                }
            }

            车头雇主责任险点击.setOnClickListener {

                ImgCheckDialog(this,bean?.tractor_employer_insurance_pic,"雇主责任险",bean?.tractor_employer_insurance_expire_date).show()
            }

        }

        if (!EmptyUtils.isTextEmpty(bean?.tractor_compensate_insurance_expire_date)){
            车头超赔险.text=bean?.tractor_compensate_insurance_expire_date
        }
        if (!EmptyUtils.isTextEmpty(bean?.tractor_compensate_insurance_pic)){
            超赔险点击.text="查看"
            if (TextUtils.isEmpty(bean?.tractor_compensate_insurance_expire_date)){

            }else{
                if (TimeUtils.timeLeft(TimeUtils.getTime(bean?.tractor_compensate_insurance_expire_date))){
                    超赔险点击.text="即将到期"
                    超赔险点击.setTextColor(Color.RED)
                }
            }
            超赔险点击.setOnClickListener {
                ImgCheckDialog(this,bean?.tractor_compensate_insurance_pic,"超赔险",bean?.tractor_compensate_insurance_expire_date).show()

            }
        }
        if (!EmptyUtils.isTextEmpty(bean?.trailer_no)){
            挂车牌号.text=bean?.trailer_no
        }

        if (!EmptyUtils.isTextEmpty(bean?.trailer_pic)){
            挂车照片点击.setText("查看")
            //todo

            挂车照片点击.setOnClickListener {
                ImgCheckDialog(this,bean?.trailer_pic,"","").show()
            }

        }

        if (!EmptyUtils.isTextEmpty(bean?.trailer_factory_model)){
            挂车型号.text=bean?.trailer_factory_model
        }

        if (!EmptyUtils.isTextEmpty(bean?.trailer_engine_no)){
            挂车发动机号.text=bean?.trailer_engine_no
        }
        if (!EmptyUtils.isTextEmpty(bean?.trailer_frame_no)){
            挂车车架号.text=bean?.trailer_frame_no
        }
        if (!EmptyUtils.isTextEmpty(bean?.trailer_register_date)){
            挂车入户时间.text=bean?.trailer_register_date
        }
        if (!EmptyUtils.isTextEmpty(bean?.trailer_passport_date)){
            挂车证注册时间.text=bean?.trailer_passport_date
        }
        if (!EmptyUtils.isTextEmpty(bean?.trailer_passport_expire_date)){
            挂车证到期时间.text=bean?.trailer_passport_expire_date
        }
        if (!EmptyUtils.isTextEmpty(bean?.trailer_passport_pic)){
            //todo
            挂车证点击.text="查看"
            if (TextUtils.isEmpty(bean?.trailer_passport_expire_date)){

            }else{

                if (TimeUtils.timeLeft(TimeUtils.getTime(bean?.trailer_passport_expire_date))){
                    挂车证点击.text="即将到期"
                    挂车证点击.setTextColor(Color.RED)
                }
            }
            挂车证点击.setOnClickListener {
                ImgCheckDialog(this,bean?.trailer_pic,"挂车证",bean?.trailer_passport_expire_date).show()
            }
        }
        if (!EmptyUtils.isTextEmpty(bean?.trailer_operation_no)){
            挂车营运证号.text=bean?.trailer_operation_no
        }
        if (!EmptyUtils.isTextEmpty(bean?.trailer_operation_date)){
            挂车营运证号注册时间.text=bean?.trailer_operation_date
        }
        if (!EmptyUtils.isTextEmpty(bean?.trailer_operation_expire_date)){
            挂车营运证到期时间.text=bean?.trailer_operation_expire_date
        }

        if (!EmptyUtils.isTextEmpty(bean?.trailer_operation_pic)){
            //todo
            挂车营运证点击.text="查看"
            if (TextUtils.isEmpty(bean?.trailer_operation_expire_date)){

            }else{
                if (TimeUtils.timeLeft(TimeUtils.getTime(bean?.trailer_operation_expire_date))){
                    挂车营运证点击.text="即将到期"
                    挂车营运证点击.setTextColor(Color.RED)
                }
            }
            挂车营运证点击.setOnClickListener {
                ImgCheckDialog(this,bean?.trailer_pic,"营运证",bean?.trailer_operation_expire_date).show()
            }

        }

        if (!EmptyUtils.isTextEmpty(bean?.trailer_insurance_company)){
            挂车交强险所属公司.text=bean?.trailer_insurance_company
        }
        if (!EmptyUtils.isTextEmpty(bean?.trailer_insurance_expire_date)){
            挂车交强险到期日期.text=bean?.trailer_insurance_expire_date
        }

        if (!EmptyUtils.isTextEmpty(bean?.trailer_insurance_pic)){
                    //todo
            挂车交强险点击.text="查看"
            if (TextUtils.isEmpty(bean?.trailer_insurance_expire_date)){

            }else{
                if (TimeUtils.timeLeft(TimeUtils.getTime(bean?.trailer_insurance_expire_date))){
                    挂车交强险点击.text="即将到期"
                    挂车交强险点击.setTextColor(Color.RED)
                }
            }
            挂车交强险点击.setOnClickListener {
                ImgCheckDialog(this,bean?.trailer_insurance_pic,"交强险",bean?.trailer_insurance_expire_date).show()
            }

        }
        if (!EmptyUtils.isTextEmpty(bean?.trailer_commercial_insurance_expire_date)){
            挂车商业险到期日期.text=bean?.trailer_commercial_insurance_expire_date
        }
        if (!EmptyUtils.isTextEmpty(bean?.trailer_commercial_insurance_pic)){
            //todo
            挂车商业险点击.text="查看"
            if (TextUtils.isEmpty(bean?.trailer_commercial_insurance_expire_date)){

            }else{
                if (TimeUtils.timeLeft(TimeUtils.getTime(bean?.trailer_commercial_insurance_expire_date))){
                    挂车商业险点击.text="即将到期"
                    挂车商业险点击.setTextColor(Color.RED)
                }

            }
            挂车商业险点击.setOnClickListener {

                ImgCheckDialog(this,bean?.trailer_commercial_insurance_pic,"商业险",bean?.trailer_commercial_insurance_expire_date).show()
            }

        }
        if (!EmptyUtils.isTextEmpty(bean?.trailer_tank_expire_date)){
            挂车罐检到期日期.text=bean?.trailer_tank_expire_date
        }

        if (!EmptyUtils.isTextEmpty(bean?.trailer_tank_pic)){
            //todo
            挂车罐检点击.text="查看"
            if (TextUtils.isEmpty(bean?.trailer_tank_expire_date)){

            }else{
                if (TimeUtils.timeLeft(TimeUtils.getTime(bean?.trailer_tank_expire_date))){
                    挂车罐检点击.text="即将到期"
                    挂车罐检点击.setTextColor(Color.RED)
                }
            }
            挂车罐检点击.setOnClickListener {

                ImgCheckDialog(this,bean?.trailer_tank_pic,"挂车罐",bean?.trailer_tank_expire_date).show()
            }

        }
        if (!EmptyUtils.isTextEmpty(bean?.trailer_tanker_book_date)){
            槽车注册日期.text=bean?.trailer_tanker_book_date
        }
        if (!EmptyUtils.isTextEmpty(bean?.trailer_tanker_book_expire_date)){
            槽车到期时间.text=bean?.trailer_tanker_book_expire_date
        }
        if (!EmptyUtils.isTextEmpty(bean?.trailer_tanker_book_pic)){
            槽车点击.setOnClickListener {
                ImgCheckDialog(this,bean?.trailer_tanker_book_pic,"槽车",bean?.trailer_tanker_book_expire_date).show()
            }
            槽车点击.text="查看"
            if (TextUtils.isEmpty(bean?.trailer_tanker_book_expire_date)){

            }else{

                if (TimeUtils.timeLeft(TimeUtils.getTime(bean?.trailer_tanker_book_expire_date))){
                    槽车点击.text="即将到期"
                    槽车点击.setTextColor(Color.RED)
                }
            }
            //todo
        }
        if (!EmptyUtils.isTextEmpty(bean?.trailer_media_info)){
            介质信息.text=bean?.trailer_media_info
        }
    }

    override fun initView() {

    }

    override fun logicStart() {
        presenter?.getCarInfo(this,SpHelper.getDriver(this)?.data?.vehicle_id?.toInt()!!)
    }
    override fun setListener() {
        log("fhp","高${carManHegith}")
        btCarMan.setOnClickListener {
            isCarMan=!isCarMan
            doShowCarMan()
        }
        btCarHead.setOnClickListener {
            isCarHead=!isCarHead
            doShowCarHead()
        }

        btGuaChe.setOnClickListener {
            isGuaChe=!isGuaChe
            doGuaChe()
        }
        btExit.setOnClickListener {
            finish()
        }
    }

    override fun onWindowFocusChanged(hasFocus: Boolean) {
        super.onWindowFocusChanged(hasFocus)
        if (isfirst){
            //车人高度处理
            carManHegith=loCarMan.height
            Log.e("fhp","高度为${carManHegith}")
            val params = loCarMan.layoutParams as LinearLayout.LayoutParams
            params.height=0
            loCarMan.layoutParams=params

            //车头高度处理
            carHeadHeight=loCarHead.height
            val carheadP = loCarHead.layoutParams as LinearLayout.LayoutParams
            carheadP.height=0
            loCarHead.layoutParams=carheadP

            //挂车高度处理
            guacheHeight=loGuaChe.height
            val guacheP = loGuaChe.layoutParams as LinearLayout.LayoutParams
            guacheP.height=0
            loGuaChe.layoutParams=guacheP

            isfirst=false
        }

    }
    private fun doShowCarMan(){
        if (isCarMan){
            val params = loCarMan.layoutParams as LinearLayout.LayoutParams
            val animator = ObjectAnimator.ofInt(0, carManHegith)
            animator.duration=200
            animator.addUpdateListener(object : ValueAnimator.AnimatorUpdateListener{
                override fun onAnimationUpdate(p0: ValueAnimator?) {
                    params.height= p0?.animatedValue as Int
                    loCarMan.layoutParams=params
                }
            })
            animator.start()
            imgCarMan.setImageResource(R.mipmap.up)
        }else{
            val params = loCarMan.layoutParams as LinearLayout.LayoutParams
            val animator = ObjectAnimator.ofInt(carManHegith, 0)
            animator.duration=200
            animator.addUpdateListener(object : ValueAnimator.AnimatorUpdateListener{
                override fun onAnimationUpdate(p0: ValueAnimator?) {
                    params.height= p0?.animatedValue as Int
                    loCarMan.layoutParams=params
                }
            })
            imgCarMan.setImageResource(R.mipmap.down)

            animator.start()
        }
    }
    private fun doGuaChe(){
        if (isGuaChe){
            val params = loGuaChe.layoutParams as LinearLayout.LayoutParams
            val animator = ObjectAnimator.ofInt(0, guacheHeight)
            animator.duration=200
            animator.addUpdateListener(object : ValueAnimator.AnimatorUpdateListener{
                override fun onAnimationUpdate(p0: ValueAnimator?) {
                    params.height= p0?.animatedValue as Int
                    loGuaChe.layoutParams=params
                }
            })
            animator.start()
            imgGuaChe.setImageResource(R.mipmap.up)
        }else{
            val params = loGuaChe.layoutParams as LinearLayout.LayoutParams
            val animator = ObjectAnimator.ofInt(guacheHeight, 0)
            animator.duration=200
            animator.addUpdateListener(object : ValueAnimator.AnimatorUpdateListener{
                override fun onAnimationUpdate(p0: ValueAnimator?) {
                    params.height= p0?.animatedValue as Int
                    loGuaChe.layoutParams=params
                }
            })
            imgGuaChe.setImageResource(R.mipmap.down)

            animator.start()
        }
    }
    private fun doShowCarHead(){
        if (isCarHead){
            val params = loCarHead.layoutParams as LinearLayout.LayoutParams
            val animator = ObjectAnimator.ofInt(0, carHeadHeight)
            animator.duration=200
            animator.addUpdateListener(object : ValueAnimator.AnimatorUpdateListener{
                override fun onAnimationUpdate(p0: ValueAnimator?) {
                    params.height= p0?.animatedValue as Int
                    loCarHead.layoutParams=params
                }
            })
            animator.start()
            imgCarHead.setImageResource(R.mipmap.up)
        }else{
            val params = loCarHead.layoutParams as LinearLayout.LayoutParams
            val animator = ObjectAnimator.ofInt(carHeadHeight, 0)
            animator.duration=200
            animator.addUpdateListener(object : ValueAnimator.AnimatorUpdateListener{
                override fun onAnimationUpdate(p0: ValueAnimator?) {
                    params.height= p0?.animatedValue as Int
                    loCarHead.layoutParams=params
                }
            })
            imgCarHead.setImageResource(R.mipmap.down)
            animator.start()
        }
    }
    override fun bindView(): CarView {
        return this
    }

    override fun createPresenter(): CarPresneter {
        return CarPresneter()
    }
}